package com.sangeng.service.impl;

import com.sangeng.domain.CaptchaVO;
import com.sangeng.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class CaptchaService {

    /**
     * 设置验证码uuid 在redis中的过期时间 2分钟
     */
    private static final Integer EXPIRY_TIME = 5;

    @Autowired
    private RedisCache redisUtils;

    private final String CAPTCHA_KEY = "captcha:verification:";

    public CaptchaVO cacheCaptcha(String captcha){
        //生成一个随机标识符
        String captchaKey = UUID.randomUUID().toString();

        //缓存验证码并设置过期时间
        redisUtils.setCacheObject(CAPTCHA_KEY.concat(captchaKey),captcha,EXPIRY_TIME, TimeUnit.MINUTES);

        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaKey(captchaKey);
        captchaVO.setExpire((long) (EXPIRY_TIME * 60));

        return captchaVO;
    }

}
